package com.wzc.org.service.impl;

import com.wzc.basic.service.impl.BaseServiceImpl;
import com.wzc.org.domain.Employee;
import com.wzc.org.mapper.EmployeeMapper;
import com.wzc.org.service.IEmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


/**
 * 业务实现类：部门
 */
@Service // 交给spring管理
public class EmployeeServiceImpl extends BaseServiceImpl<Employee> implements IEmployeeService {


    @Autowired
    private EmployeeMapper employeeMapper;

    @Override
    @Transactional
    public void save(Employee employee){
        // 先保存自己 t_employee 返回自增主键
        employeeMapper.save(employee);
        // 再保存中间表 t_employee_role 的数据（关联表）
        employeeMapper.saveEmployeeRole(employee.getId(), employee.getRole_id());
    }

    @Override
    @Transactional
    public void remove(Long id){
        // 先删中间表 t_employee_role 的数据（关联表）
        employeeMapper.removeEmployeeRole(id);
        // 再删自己表中数据 t_employee
        employeeMapper.remove(id);
    }

    @Override
    @Transactional
    public void update(Employee employee){
        // 先将自己表中的数据修改
        super.update(employee);
        // 再根据id删除中间表 t_employee_role 的数据
        employeeMapper.removeEmployeeRole(employee.getId());
        // 最后再将修改后的数据同步到 t_employee_role
        employeeMapper.saveEmployeeRole(employee.getId(), employee.getRole_id());
    }


    @Override
    public List<Employee> loadByShopId(Long id) {
        return employeeMapper.loadByShopId(id);
    }
}
